" solarized主题
let g:current_theme_file = expand('~/.vim/vimrcs/themes/current_theme.vim')
function! Set_current_theme(theme_name)
    " 获取第一个参数作为内容
    let l:theme_name = a:theme_name
    " 获取第二个参数作为文件路径
    let l:file_path = g:current_theme_file

    " 使用 writefile() 函数将内容写入指定的文件
    call writefile([l:theme_name], l:file_path)
endfunction

function! Mytheme_solarized(theme)
  syntax enable
  if a:theme == 'light'
    set background=light
  elseif a:theme == 'dark'
    set background=dark
  else
    echo "Invalid theme: choose 'light' or 'dark'"
    return
  endif
  colorscheme solarized
endfunction

function! Mytheme_solarized_arg_complete(ArgLead, CmdLine, CursorPos)
  return ['light', 'dark']
endfunction

" nord主题
function! Mytheme_nord()
  colorscheme nord
endfunction

" dracula主题
function! Mytheme_dracula()
  colorscheme dracula
endfunction

" nord主题
function! Mytheme_gruvbox()
  "set background=light
  set background=dark
  colorscheme gruvbox
endfunction

" onedark主题
function! Mytheme_onedark()
  colorscheme onedark
endfunction

" 自定义命令
" nargs表示接受一个参数，complete表示要支持自动补全，使用自定义的函数输出一个列表
command! -nargs=1 -complete=customlist,Mytheme_solarized_arg_complete MythemeSolarized call Mytheme_solarized(<f-args>) |call Set_current_theme('MythemeSolarized')
command! MythemeNord call Mytheme_nord() | call Set_current_theme('MythemeNord')
command! MythemeDracula call Mytheme_dracula() | call Set_current_theme('MythemeDracula')
command! MythemeGruvbox call Mytheme_gruvbox() | call Set_current_theme('MythemeGruvbox')
command! MythemeOnedark call Mytheme_onedark() | call Set_current_theme('MythemeOnedark')

" 每次进入新的缓冲区都判断当前是否为diff mode，如果是的话，选择gruvbox主题
autocmd BufWinEnter * if &diff | call Mytheme_gruvbox() | endif
autocmd BufEnter * if &diff | call Mytheme_gruvbox() | endif
" 每次进入vim，都设置默认的主题色,如果默认主题色不存在，则默认使用Dracula
function! LoadCurrentTheme()
    if !filereadable(g:current_theme_file)
        MythemeDracula
    else
        execute 'source' g:current_theme_file
    endif
endfunction

command! LoadCurrentTheme call LoadCurrentTheme()
LoadCurrentTheme

